﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Ldy.Core
{
    /// <summary>
    /// lil 通用功能类 {21EB22A9-1A47-4EDB-BD53-FE030755C7D8}
    /// </summary>
    class Cmn
    {
        #region lil 文件压缩解压功能
        /// <summary>
        /// 文件压缩
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool FileCompress(string filePath)
        {
            Log.Info("【文件压缩开始】文件路径为：" + filePath);
            try
            {
                //using (ZipOutputStream zipstream = new ZipOutputStream(File.Create(filePath + ".zip")))
                //{
                //    zipstream.SetLevel(9);
                //    byte[] buffer = new byte[4096];
                //    ZipEntry entry = new ZipEntry(Path.GetFileName(filePath));
                //    entry.DateTime = DateTime.Now;
                //    zipstream.PutNextEntry(entry);
                //    using (FileStream fs = File.OpenRead(filePath))
                //    {
                //        int sourceBytes;
                //        do
                //        {
                //            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                //            zipstream.Write(buffer, 0, sourceBytes);
                //        }
                //        while (sourceBytes > 0);
                //    }
                //    zipstream.Finish();
                //    zipstream.Close();
                //    Info("【文件压缩结束】");
                //    return true;
                //}
                return true;
            }
            catch (Exception ex)
            {
                Log.Error("【文件压缩错误】：" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 文件解压
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool FileDecompress(string filePath)
        {
            Log.Info("【文件解压开始】文件路径为：" + filePath);
            if (!File.Exists(filePath))
            {
                Log.Error("【文件解压-文件不存在！】");
                return false;
            }
            try
            {
                //using (ZipInputStream inputStream = new ZipInputStream(File.OpenRead(filePath)))
                //{
                //    ZipEntry theEntry;
                //    while ((theEntry = inputStream.GetNextEntry()) != null)
                //    {
                //        if (!string.IsNullOrEmpty(theEntry.Name))
                //        {
                //            using (FileStream streamWriter = File.Create(Path.Combine(Path.GetDirectoryName(filePath), theEntry.Name)))
                //            {
                //                int size = 2048;
                //                byte[] data = new byte[2048];
                //                while (true)
                //                {
                //                    size = inputStream.Read(data, 0, data.Length);
                //                    if (size > 0)
                //                    {
                //                        streamWriter.Write(data, 0, size);
                //                    }
                //                    else
                //                    {
                //                        break;
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
                return true;
            }
            catch (Exception ex)
            {
                Log.Error("【文件解压错误】：" + ex.Message);
                return false;
            }
            Log.Info("【文件解压结束】");
            return true;
        }
        #endregion

    }
}
